package io.osw.modules.sys.service.impl;

import io.osw.common.utils.Constant;
import io.osw.modules.sys.entity.TblCompanyEntity;
import io.osw.modules.sys.entity.TblMtlItemEntity;
import io.osw.modules.sys.service.TblCompanyService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.osw.common.utils.PageUtils;
import io.osw.common.utils.Query;

import io.osw.modules.sys.dao.TblVendorDao;
import io.osw.modules.sys.entity.TblVendorEntity;
import io.osw.modules.sys.service.TblVendorService;


@Service("tblVendorService")
public class TblVendorServiceImpl extends ServiceImpl<TblVendorDao, TblVendorEntity> implements TblVendorService {

    @Autowired
    private TblCompanyService tblCompanyService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String vendorName = (String)params.get("vendorName");
        Page<TblVendorEntity> page = this.selectPage(
                new Query<TblVendorEntity>(params).getPage(),
                new EntityWrapper<TblVendorEntity>()
                .like(StringUtils.isNotBlank(vendorName),"vendor_name", vendorName)
                        .addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
        );


        List<TblVendorEntity> records = page.getRecords();

        for (TblVendorEntity record : records) {
            if (record == null) {
                return null;
            }

            //注入公司名
            TblCompanyEntity tblCompanyEntity = tblCompanyService.selectById(record.getCompanyId());
            record.setCompanyName(tblCompanyEntity.getCompanyName());


        }

        return new PageUtils(page);
    }

}
